package com.ruoyi.energy.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.energy.domain.NyMeterAccount2;
import com.ruoyi.energy.domain.vo.NyMeterAccountExcelVO1;
import com.ruoyi.energy.service.INyMeterAccount2Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 仪台账运营Controller
 *
 * @author cjie
 * @date 2022-02-25
 */
@Api(tags = " 仪台账运营")
@RestController
@RequestMapping("/nyMeterAccount2")
public class NyMeterAccount2Controller extends BaseController {
    @Autowired
    private INyMeterAccount2Service nyMeterAccount2Service;

    /**
     * 查询仪台账运营列表
     */
    @ApiOperation(value="查询仪台账运营列表", notes="查询仪台账运营列表")
    @PreAuthorize(hasPermi = "energy:nyMeterAccount2:list")
    @GetMapping("/list")
    public TableDataInfo list(NyMeterAccount2 nyMeterAccount2) {
        startPage();
        List<NyMeterAccount2> list = nyMeterAccount2Service.selectNyMeterAccount2List(nyMeterAccount2);
        return getDataTable(list);
    }

    /**
     * 查询仪台账运营列表
     */
    @ApiOperation(value="扫码获取信息", notes="扫码获取信息")
    @GetMapping("/scan")
    public AjaxResult getInfoByMeterNum(NyMeterAccount2 nyMeterAccount2) {
        return AjaxResult.success(nyMeterAccount2Service.selectNyMeterAccount2ByMeterNum(nyMeterAccount2));
    }

    /**
     * 导出仪台账运营列表
     */
    @ApiOperation(value="导出仪台账运营列表", notes="导出仪台账运营列表")
    @PreAuthorize(hasPermi = "energy:nyMeterAccount2:export")
    @Log(title = "仪台账运营", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, NyMeterAccount2 nyMeterAccount2) throws IOException {
        List<NyMeterAccount2> list = nyMeterAccount2Service.selectNyMeterAccount2List(nyMeterAccount2);
        ExcelUtil<NyMeterAccount2> util = new ExcelUtil<NyMeterAccount2>(NyMeterAccount2.class);
        util.exportExcel(response, list, "仪台账运营数据");
    }
    /**
     * 获取仪台账运营详细信息
     */
    @ApiOperation(value="获取仪台账运营详细信息", notes="获取仪台账运营详细信息")
    @PreAuthorize(hasPermi = "energy:nyMeterAccount2:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(nyMeterAccount2Service.selectNyMeterAccount2ById(id));
    }

    /**
     * 新增仪台账运营
     */
    @ApiOperation(value="新增仪台账运营", notes="新增仪台账运营")
    @PreAuthorize(hasPermi = "energy:nyMeterAccount2:add")
    @Log(title = "仪台账运营", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NyMeterAccount2 nyMeterAccount2) {
        return toAjax(nyMeterAccount2Service.insertNyMeterAccount2(nyMeterAccount2));
    }

    /**
     * 修改仪台账运营
     */
    @ApiOperation(value="修改仪台账运营", notes="修改仪台账运营")
    @PreAuthorize(hasPermi = "energy:nyMeterAccount2:edit")
    @Log(title = "仪台账运营", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NyMeterAccount2 nyMeterAccount2) {
        return toAjax(nyMeterAccount2Service.updateNyMeterAccount2(nyMeterAccount2));
    }

    /**
     * 删除仪台账运营
     */
    @ApiOperation(value="删除仪台账运营", notes="删除仪台账运营")
    @ApiImplicitParam(name = "ids", value = "仪台账运营ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "energy:nyMeterAccount2:remove")
    @Log(title = "仪台账运营", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(nyMeterAccount2Service.deleteNyMeterAccount2ByIds(ids));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<NyMeterAccountExcelVO1> util = new ExcelUtil<NyMeterAccountExcelVO1>(NyMeterAccountExcelVO1.class);
        util.importTemplateExcel(response, "仪表数据");
    }

    /**
     * 导入抄表记录列表,
     */
    @PreAuthorize(hasPermi = "energy:nyMeterAccount2:edit")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, String meterType) throws Exception {
        ExcelUtil<NyMeterAccountExcelVO1> util = new ExcelUtil<NyMeterAccountExcelVO1>(NyMeterAccountExcelVO1.class);
        List<NyMeterAccountExcelVO1> buiRoomVOList = util.importExcel(file.getInputStream());
        //批量插入数据
        String message = nyMeterAccount2Service.importData(buiRoomVOList,meterType);
        return AjaxResult.success(message);

    }
}
